package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppAuth;
import com.vf.admin.miaodong.entity.MessageChatFrom;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class MessageChatFromDao extends BaseDao<MessageChatFrom> {

    public List<AppAuth> page(Map<String, Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_auth where 1=1");
        if (StringEx.isNotNull(paraMap.get("authName"))) {
            sbStr.append(" and authName = ? ");
            paramList.add(paraMap.get("authName"));
        }
        if (StringEx.isNotNull(paraMap.get("authNameEn"))) {
            sbStr.append(" and authNameEn = ? ");
            paramList.add(paraMap.get("authNameEn"));
        }

        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppAuth.class, splitPage);
    }

    public void del(Integer id) {
        if (id==null || id <= 0) {
            return;
        }
        String sql = "delete from app_auth where id = ?";
        ArrayList<Object> paramList = new ArrayList<>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete from message_chat_from where fromUserId = ? or toUserId = ?";
        ArrayList<Object> paramList = new ArrayList<>();
        paramList.add(userId);
        paramList.add(userId);
        this.del(sql, paramList);
    }

    public MessageChatFrom get(Integer fromUserId, Integer toUserId) {
        ArrayList<Object> params = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from message_chat_from where (fromUserId=? and toUserId=?) or (fromUserId=? and toUserId=?) limit 1");
        String sql = sbStr.toString();
        params.add(fromUserId);
        params.add(toUserId);
        params.add(toUserId);
        params.add(fromUserId);

        return this.queryObj(sql, params, MessageChatFrom.class);
    }

    public MessageChatFrom getByFrom(Integer fromUserId, Integer toUserId) {
        ArrayList<Object> params = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from message_chat_from where fromUserId=? and toUserId=?");
        String sql = sbStr.toString();
        params.add(fromUserId);
        params.add(toUserId);

        return this.queryObj(sql, params, MessageChatFrom.class);
    }

    public List<MessageChatFrom> getByUserId(Integer userId) {
        ArrayList<Object> params = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from message_chat_from where fromUserId=? or toUserId=?");
        String sql = sbStr.toString();
        params.add(userId);
        params.add(userId);

        return this.queryList(sql, params, MessageChatFrom.class);
    }

}
